// Copyright 2001, 2002, 2003, 2004, 2005 Macromedia, Inc. All rights reserved

var helpDoc = MM.HELP_objHyperlink;

//---------------   GLOBAL CONSTANTS   ---------------

var DEFAULT_TARGETS = new Array("_blank","");


//---------------   GLOBAL VARIABLES   ---------------

var TMS_TITLE;
var TMS_GROUP;
var LIST_LINKS;
var TEXT_TITLE;
var DOC_TARGETS = new Array(); // Targets gathered from document. 

//---------------     API FUNCTIONS    ---------------

function isDOMRequired() { 
	// Return false, indicating that this object is available in code view.
	return false;
}

function objectTag(){
	var dom = dw.getDocumentDOM();
  var linkStr = '<cms:text title="' + TMS_TITLE.value + '" group="' + TMS_GROUP.value + '"';
  linkStr += '><span';
	if (TEXT_TITLE && TEXT_TITLE.value != ""){
		linkStr += ' title="' + TEXT_TITLE.value + '"';
	}
    linkStr += '>$!text#*' + TMS_TITLE.value + TMS_GROUP.value +'*#</span></cms:text>';
	  
	if (dw.getFocus() == 'html' || dw.getFocus() == 'textView'){
		var range = dom.source.getSelection();
		dom.source.replaceRange(range[0],range[1],linkStr);
	}else if ((dom.getSelection()[0] != dom.getSelection()[1]) && isCurSelectionTextOnly(dom)){
    dom.insertHTML(linkStr,true);  
	}else if ((dom.getSelection()[0] != dom.getSelection()[1]) && isCurTagXSLValueOf(dom)){
		dom.insertHTML(linkStr,true);  
  }else{
		dom.insertHTML(linkStr,false);
	}
	
	return;
}

//---------------    LOCAL FUNCTIONS   ---------------
function initUI() {
	var dom = dw.getDocumentDOM();

	
	TMS_TITLE = document.theForm.linkTMSTitle;
	TMS_GROUP = document.theForm.linkTMSGroup;
	TEXT_TITLE = document.theForm.linkTitle;
	
	
	// If we're in code view, automatically populate the
	// Text field with the selected text, and the link field
	// with the selected text if it appears to be a link.
	if (dw.getFocus() == 'html' || dw.getFocus() == 'textView'){
		// populate Text field with selection even if it includes
		// tags
		var theSel = dom.source.getSelection();
		
	}

}


// Description: Determines if the current selection is contained within a text node.
// Parameters:  DOM - checked for valid, returns false if no DOM.
function isCurSelectionTextOnly(curDOM) {
  var rtnBool = false;
  if (curDOM != null) {
    var curNode = curDOM.getSelectedNode();
    if (curNode.nodeType == Node.TEXT_NODE) { // Return true if we are a text node.
      rtnBool = true;
    } else { // Return true if the selection contains a single text node.
      if (curNode.hasChildNodes() && curNode.childNodes[0].nodeType == Node.TEXT_NODE) {
        var curSel = curDOM.getSelection();
        var nodeOffset = dw.nodeToOffsets(curNode.childNodes[0]);
        if  ((nodeOffset[0] <= curSel[0]) && (curSel[1] <= nodeOffset[1])) {
          rtnBool = true;
  } } } }
  return rtnBool;
}

//is the cur tag xsl:value-of
function isCurTagXSLValueOf(curDOM)
{
  var rtnBool = false;
  if (curDOM != null) 
	{
    var curNode = curDOM.getSelectedNode();
		if ((curNode != null) && (curNode.nodeType == Node.ELEMENT_NODE))
		{
				var curTagName = curNode.tagName;
				curTagName = curTagName.toLowerCase();
				if (curTagName == "xsl:value-of")
				{
					rtnBool = true;
				}
		}
	}
	return rtnBool;
}

